﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors;
using Newtonsoft.Json.Linq;

namespace LdCms.Web.Controllers.API.Community.V1
{
    using LdCms.EF.DbModels;
    using LdCms.IBLL.Member;
    using LdCms.IBLL.Community;
    using LdCms.Web.Services;

    /// <summary>
    /// 
    /// </summary>
    [ApiVersion("1.0")]
    [ControllerName("community/subscribe")]
    public class CommunitySubscribeController : BaseApiController
    {
        private readonly IBaseApiManager BaseApiManager;
        private readonly IAccountService AccountService;
        private readonly ISubscribeService SubscribeService;
        public CommunitySubscribeController(IBaseApiManager BaseApiManager, IAccountService AccountService, ISubscribeService SubscribeService) : base(BaseApiManager)
        {
            this.BaseApiManager = BaseApiManager;
            this.AccountService = AccountService;
            this.SubscribeService = SubscribeService;
        }

        [HttpPost]
        [ActionName("add")]
        public IActionResult SaveSubscribe(string access_token, [FromBody]JObject fromValue)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(access_token, fromValue);
                int systemId = MemberAccount.SystemID;
                string companyId = MemberAccount.CompanyID;
                string memberId = MemberAccount.MemberID;
                string subscribeUserId = GetJObjectValue(fromValue, "subscribe_user_id");
                if (string.IsNullOrWhiteSpace(subscribeUserId))
                    return Error(logId, "subscribe_user_id not empty！");
                if (memberId == subscribeUserId)
                    return Error(logId, "不能关注自已！");
                if (!AccountService.IsExists(systemId, companyId, subscribeUserId))
                    return Error(logId, "subscribeUserId invalid！");
                var result = SubscribeService.SaveSubscribe(systemId, companyId, memberId, subscribeUserId);
                if (result)
                    return Success(logId, "ok");
                else
                    return Error(logId, "fail");
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="fromValue"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("delete")]
        public IActionResult DeleteThemeCollect(string access_token, [FromBody]JObject fromValue)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(access_token, fromValue);
                int systemId = MemberAccount.SystemID;
                string companyId = MemberAccount.CompanyID;
                string memberId = MemberAccount.MemberID;
                string subscribeUserId = GetJObjectValue(fromValue, "subscribe_user_id");
                if (string.IsNullOrWhiteSpace(subscribeUserId))
                    return Error(logId, "subscribe_user_id not empty！");
                if (!SubscribeService.IsExists(systemId, companyId, memberId, subscribeUserId))
                    return Error(logId, "theme_id invalid！");
                var result = SubscribeService.DeleteSubscribe(systemId, companyId, memberId, subscribeUserId);
                if (result)
                    return Success(logId, "ok");
                else
                    return Error(logId, "fail");
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }


        private bool IsSaveThemeCollectParams(JObject formValue)
        {
            try
            {
                if (formValue == null)
                    throw new Exception("params not empty！");
                if (formValue.Property("subscribe_user_id") == null)
                    throw new Exception("lack subscribe_user_id params！");
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }



    }
}